package org.huel.backend.controller;

import org.huel.backend.domain.dto.StudentScoreDTO;
import org.huel.backend.domain.entity.CourseAttendance;
import org.huel.backend.service.impl.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController

public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 获取所有课程
     */
    @GetMapping("/courses")
    public ResponseEntity<List<CourseAttendance>> getAllCourses() {
        List<CourseAttendance> courses = courseService.getAllCourses();
        return ResponseEntity.ok(courses);
    }

    /**
     * 获取特定课程的出勤统计
     */
    @GetMapping("/courses/{courseName}/attendance")
    public ResponseEntity<Map<String, Object>> getCourseAttendanceSummary(
            @PathVariable String courseName) {
        System.out.println("Fetching attendance summary for course: " + courseName);
        Map<String, Object> summary = courseService.getCourseAttendanceSummary(courseName);
        return ResponseEntity.ok(summary);
    }

    /**
     * 获取特定课程的学生成绩排名
     */
    @GetMapping("/courses/{courseName}/top-students")
    public ResponseEntity<List<StudentScoreDTO>> getTopStudentsByCourse(
            @PathVariable String courseName,
            @RequestParam(defaultValue = "5") int limit) {
        List<StudentScoreDTO> students = courseService.getTopStudentsByCourse(courseName, limit);
        return ResponseEntity.ok(students);
    }
}
